﻿' ----------------------------------------------------------------------
'
' FileSystem.vb
'
'   Dbfs.Domain
'
'   Exposes instance methods for creating, moving, and enumerating through 
'   directories and subdirectories. This class cannot be inherited.
'
'   Use the DirectoryInfo class for typical operations such as copying, 
'   moving, renaming, creating, and deleting directories.
'
' ----------------------------------------------------------------------
' version:  1.0.0.1
' today:    09/05/10  10:16:00
' ident:    "@(#)/Dbfs/Domain/FileSystem.vb 1.0 0.1 09/05/10 0 rjs"

Imports System.IO


Namespace Dbfs

    ''' <summary>
    ''' (DBFS) Exposes instance methods for creating, moving, and enumerating through 
    ''' directories and subdirectories. This class cannot be inherited.
    ''' </summary>
    ''' <remarks>Use the DirectoryInfo class for typical operations such as 
    ''' copying, moving, renaming, creating, and deleting directories.</remarks>
    <SerializableAttribute()> _
    Public Class FileSystem

        Private _children As NodeCollection

        ' class variables
        Private _root As DirectoryInfo


#Region " Constructors "
        Public Sub New()
        End Sub
#End Region ' Constructors

#Region " Properties "
        ''' <summary>
        ''' Indicates whether the hierarchical data node that the IHierarchyData 
        ''' object represents has any child nodes.
        ''' </summary>
        ''' <value><b>true</b> if the current node has child nodes; otherwise, <b>false</b>.</value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Drives() As Boolean
            Get
            End Get
        End Property

#End Region ' Properties

#Region " Methods "
        Public Shared Sub CopyDirectory( _
            ByVal sourceDirectoryName As String, _
            ByVal destinationDirectoryName As String _
            )
            CopyDirectory(sourceDirectoryName, destinationDirectoryName, False)
        End Sub

        ''' <summary>
        ''' Copies the contents of a directory to another directory.
        ''' </summary>
        ''' <param name="sourceDirectoryName">The directory to be copied.</param>
        ''' <param name="destinationDirectoryName">The location to which the directory contents should be copied.</param>
        ''' <param name="overwrite"><b>True</b> to overwrite existing files; otherwise <b>False</b>. Default is <b>False</b></param>
        ''' <remarks>This method copies the contents of the source directory to the target directory. 
        ''' If the target directory does not exist, it is created. If a directory with the same name 
        ''' exists in the target location, the contents of the two directories are merged. 
        ''' You can specify a new name for the directory during the operation.
        ''' <para>
        ''' When copying files within a directory, exceptions may be thrown that are caused by a 
        ''' specific file, such as a file existing during a merge while overwrite is set to False. 
        ''' When such exceptions are thrown, they are consolidated into a single exception whose 
        ''' Data property holds entries in the form of an IDictionary in which the file or directory 
        ''' path is the key and the specific exception message is contained in the corresponding value. 
        ''' Use For…Each to enumerate through the entries.</para>
        ''' </remarks>
        Public Shared Sub CopyDirectory( _
            ByVal sourceDirectoryName As String, _
            ByVal destinationDirectoryName As String, _
            ByVal overwrite As Boolean _
            )

            ' local variables
            Dim sourceDir As DirectoryInfo
            Dim destinationDir As DirectoryInfo

            sourceDir = New DirectoryInfo(sourceDirectoryName)
            destinationDir = New DirectoryInfo(destinationDirectoryName)
            destinationDir.Create()

            CopySubDirectory(sourceDir, destinationDir)

        End Sub

        Private Shared Sub CopySubDirectory(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)

            ' iterate through the source directory and copy it's child directories
            For Each item In source.GetDirectories
                Dim subdir As DirectoryInfo = target.CreateSubdirectory(item.Name)
                CopySubDirectory(item, subdir)
            Next

            ' iterate through the source directory and copy it's child files
            For Each item In source.GetFiles
                Dim fi As New FileInfo(item.FullName)
                Dim targetName As String = target.FullName & DIRECTORY_SEP_CHAR & item.Name
                fi.CopyTo(targetName)
            Next

        End Sub

        ''' <summary>
        ''' Copies a file to a new location.
        ''' </summary>
        ''' <param name="sourceFileName"></param>
        ''' <param name="destinationFileName"></param>
        ''' <param name="overwrite"></param>
        ''' <remarks></remarks>
        Public Shared Sub CopyFile( _
            ByVal sourceFileName As String, _
            ByVal destinationFileName As String, _
            ByVal overwrite As Boolean _
            )

        End Sub
#End Region ' Methods

#Region " Helper Methods "
        Public Shared Sub CopyDirectory( _
            ByVal sourceDirectory As DirectoryInfo, _
            ByVal destinationDirectory As DirectoryInfo _
            )

            ' local variables
            Dim destSubdir As DirectoryInfo
            Dim sourceSubdir As DirectoryInfo

            Dim sourceFile As FileInfo

            ' perform depth-first recursion to build directory structure
            For Each sourceSubdir In sourceDirectory.GetDirectories
                destSubdir = destinationDirectory.CreateSubdirectory(sourceSubdir.Name)
                CopyDirectory(sourceSubdir, destSubdir)
            Next

            ' copy files
            For Each sourceFile In sourceDirectory.GetFiles
                'sourceFile.c()
            Next

        End Sub

        ''' <summary>
        ''' Validates the arguments for the <see cref="CopyDirectory"/> method.
        ''' </summary>
        ''' <param name="path">The path to a directory in the filesystem.</param>
        ''' <remarks></remarks>
        Private Shared Sub ValidateArguments(ByVal path As String)

            ' validate parameters
            If path Is Nothing Then
                Throw New ArgumentNullException("searchPattern")
            End If

            If path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) > 0 Then
                Throw New ArgumentException("Invalid search pattern.")
            End If

            ' wildcards are only allowed in the leaf node of the search path
            Dim j As Int16 = path.IndexOfAny("?*")
            If j >= 0 Then _
                Throw New ArgumentException("Illegal characters in path.")

        End Sub
#End Region ' Helper Methods

    End Class

End Namespace
